package com.hank.activemq.springboot.VirtualTopic;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

/***
 * 消息生产者服务类
 */
@Service
public class Producer {

    @Autowired// 也可以注入JmsTemplate，JmsMessagingTemplate 对JmsTemplate 进行了封装
    private JmsMessagingTemplate jmsTemplate;


    public void sendMessage(Destination destination, final String msg) {
        System.out.println(Thread.currentThread().getName() + " 向队列" + destination.toString() + "发送消息->" + msg);
        jmsTemplate.convertAndSend(destination,msg);
    }


    // @Scheduled(fixedDelay = 2000)
    public void sendMessage() {
        String msg="Test Message!";
        Destination destination=new ActiveMQQueue("hank");
        System.out.println(Thread.currentThread().getName() + " 向队列" + destination + "发送消息->" + msg);
        jmsTemplate.convertAndSend(destination, msg);
    }
}